package com.example.springboot.utils.vo;

import com.example.springboot.enumInfo.EnumInfoType;
import com.example.springboot.enumInfo.impl.EnumResult;
import com.example.springboot.exceptionInfo.ExceptionInfoType;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: fengyuan
 * @date 2018年9月26日 18:13:24
 * @Description TODO 公共返回对象
 */
@Data
@Slf4j
@ApiModel(description = "公共返回对象")
public class RstData<T> implements Serializable {


    @ApiModelProperty(value = "状态码标识", dataType = "String")
    private String code;

    @ApiModelProperty(value = "业务信息", dataType = "String")
    private String msg;

    @ApiModelProperty(value = "结果集(泛型)", required = true, dataType = "object")
    private T result;

    public T getResult() {
        return result == null ? (T) new HashMap<String, Object>() : result;
    }

    private RstData() {
    }

    //==============================================================================
    public static RstData successMsg(String msg) {
        RstData rstData = new RstData();
        rstData.setCode(EnumResult.SUCCESS.getCode());
        rstData.setMsg(msg);
//        log.debug("[成功 rstData：{}]", rstData);
        return rstData;
    }

    public static RstData successMsg(String msg, Object obj) {
        RstData rstData = new RstData();
        rstData.setCode(EnumResult.SUCCESS.getCode());
        rstData.setMsg(msg);
        rstData.setResult(obj);
//        log.debug("[成功 rstData：{}]", rstData);
        return rstData;
    }

    public static RstData successMsg(String msg, String key, Object value) {
        Map<String, Object> map = new HashMap<>();
        map.put(key, value);
        RstData rstData = new RstData();
        rstData.setCode(EnumResult.SUCCESS.getCode());
        rstData.setMsg(msg);
        rstData.setResult(map);
//        log.debug("[成功 rstData：{}]", rstData);
        return rstData;
    }

    public static RstData success() {
        return successMsg(EnumResult.SUCCESS.getMsg());
    }

    public static RstData success(Object obj) {
        return successMsg(EnumResult.SUCCESS.getMsg(), obj);
    }

    public static RstData success(String key, Object value) {
        return successMsg(EnumResult.SUCCESS.getMsg(), key, value);
    }

    //==============================================================================
    private static RstData faild(String code, String msg) {
        RstData rstData = new RstData();
        rstData.setCode(code);
        rstData.setMsg(msg);
//        log.debug("[失败：{}]", rstData);
        return rstData;
    }

    public static RstData faild(EnumInfoType eit) {
        return faild(eit.getCode(), eit.getMsg());
    }

    public static RstData faild(EnumInfoType er, String msg) {
        return faild(er.getCode(), er.getMsg() + "，具体原因：" + msg);
    }

    public static RstData faild(ExceptionInfoType eit) {
        return faild(eit.getCode(), eit.getMsg());
    }

    public static RstData faild(String msg) {
        return faild(EnumResult.FAILED, msg);
    }
    //==============================================================================

}
